package ytl;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Sales {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    JComboBox search = new JComboBox();
    int xtr;
    int i;
    JScrollPane scrollpane = new JScrollPane();
    int j;
    int x;
    int ctr;
    float Total;
    float subTotal;
    float cumTotal;
    Point pt = new Point();
    Dimension D = new Dimension();
    static Dimension Dim = new Dimension();
    String Button_Command = "Add";
    String Price_new = "0";
    public static Main_Menu Menu = new Main_Menu();
    JFrame Sales_Frame = new JFrame("Add");
    JTextArea Summary = new JTextArea();
    
    JTextField Name_txt = new JTextField("");
    JTextField Category_txt = new JTextField("");
    JTextField Size_txt = new JTextField("");
    JTextField Color_txt = new JTextField("");
    JTextField Buy_Price_txt = new JTextField("");
    JTextField Picture_txt = new JTextField("");
    JTextField Quantity_txt = new JTextField("");
    JTextField Price_txt = new JTextField("");
    JLabel Error_sales = new JLabel("");
    JLabel Error_frame = new JLabel("");
    
    final static String Header[][] = {{"Name", "Category", "Size", "Color", "Buy Price", "Picture", "Quantity", "Price"}};
    final static String Insider[] = {"","","","","","","",""};
    final DefaultTableModel Cart_model = new DefaultTableModel(Header,Insider);
    JScrollPane Inventory_scrollpane = new JScrollPane();
    
    JTable Inventory_table = new  JTable(Inventory.Inventory_model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
        @Override
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            
                if(Index_row == 0){
                    comp.setBackground(Color.black);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.gray);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 1 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.white);
                    comp.setForeground(Color.black);
                } 
                else {
                    comp.setBackground(Color.red);
                    comp.setForeground(Color.white);
                }
            return comp;
        }
    };
    
    JScrollPane Cart_scrollpane = new JScrollPane();
    JTable Cart_table = new JTable(Cart_model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
        @Override
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            
                if(Index_row == 0){
                    comp.setBackground(Color.black);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.gray);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 1 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.white);
                    comp.setForeground(Color.black);
                } 
                else {
                    comp.setBackground(Color.red);
                    comp.setForeground(Color.white);
                }
            return comp;
        }
    };
    
    JLabel logo = new JLabel();
    
    final ImageIcon add_item_button_img = new ImageIcon("pictures/sales/add_cart_button.png");
    final ImageIcon add_item_buttonr_img = new ImageIcon("pictures/sales/add_cart_button_r.png");
    final ImageIcon add_item_buttonp_img = new ImageIcon("pictures/sales/add_cart_button_p.png");

    final ImageIcon delete_item_button_img = new ImageIcon("pictures/sales/remove_cart_button.png");
    final ImageIcon delete_item_buttonr_img = new ImageIcon("pictures/sales/remove_cart_button_r.png");
    final ImageIcon delete_item_buttonp_img = new ImageIcon("pictures/sales/remove_cart_button_p.png");
      
    final ImageIcon search_button_img = new ImageIcon("pictures/sales/search_item_button.png");
    final ImageIcon search_buttonr_img = new ImageIcon("pictures/sales/search_item_button_r.png");
    final ImageIcon search_buttonp_img = new ImageIcon("pictures/sales/search_item_button_p.png");

    final ImageIcon custom_price_button_img = new ImageIcon("pictures/sales/custom_price_button.png");
    final ImageIcon custom_price_buttonr_img = new ImageIcon("pictures/sales/custom_price_button_r.png");
    final ImageIcon custom_price_buttonp_img = new ImageIcon("pictures/sales/custom_price_button_p.png");

    final ImageIcon check_out_button_img = new ImageIcon("pictures/sales/checkout_button.png");
    final ImageIcon check_out_buttonr_img = new ImageIcon("pictures/sales/checkout_button_r.png");
    final ImageIcon check_out_buttonp_img = new ImageIcon("pictures/sales/checkout_button_p.png");
        
    final ImageIcon confirm_button_img = new ImageIcon("pictures/n_confirm_button.png");
    final ImageIcon confirm_buttonr_img = new ImageIcon("pictures/n_confirm_button_r.png");
    final ImageIcon confirm_buttonp_img = new ImageIcon("pictures/n_confirm_button_p.png");
    
    final ImageIcon return_button_img = new ImageIcon("pictures/n_return_button.png");
    final ImageIcon return_buttonr_img = new ImageIcon("pictures/n_return_button_r.png");
    final ImageIcon return_buttonp_img = new ImageIcon("pictures/n_return_button_p.png");
        
    JButton Add_Button = new JButton(add_item_button_img);
    JButton Delete_Button = new JButton(delete_item_button_img);
    JButton Search_Button = new JButton(search_button_img);
    JButton Custom_Price_Button = new JButton(custom_price_button_img);
    JButton Check_Out_Button = new JButton(check_out_button_img);
        
    Box Sales_Tables_box = Box.createVerticalBox();
    Box Sales_Data_box = Box.createVerticalBox();
    Box Error_box = Box.createHorizontalBox();
    
    JLabel Cart_label = new JLabel();
    JLabel Inventory_label = new JLabel();
    String str = new String();
    
    ImageIcon add_item_error_icon = new ImageIcon("pictures/error/error_select_add_cart.png");
    ImageIcon delete_item_error_icon = new ImageIcon("pictures/error/error_select_remove_cart.png");
    ImageIcon remove_cart_error_icon = new ImageIcon("pictures/error/error_select_remove_cart.png");
    ImageIcon custom_price_error_icon = new ImageIcon("pictures/error/error_select_specify_custom_price.png");
    ImageIcon blank_fields_error_icon = new ImageIcon("pictures/error/error_complete_blank_fields.png");    
    ImageIcon invalid_input_error_icon = new ImageIcon("pictures/error/error_not_valid.png");
    ImageIcon frame_spacer = new ImageIcon("pictures/error/gray.png");
    
    Container Sales_Frame_container = Sales_Frame.getContentPane();
    
    JLabel Name_label = new JLabel("Name:  ");
    JLabel Category_label = new JLabel("Category:  ");        
    JLabel Size_label = new JLabel("Size:  ");
    JLabel Color_label = new JLabel("Color:  ");
    JLabel Buy_Price_label = new JLabel("Buy Price:  ");
    JLabel Tax_label = new JLabel("Tax:  ");
    JLabel Picture_label = new JLabel("Picture:  ");
    JLabel Quantity_label = new JLabel("Quantity:  ");
    JLabel Price_label = new JLabel("Price:  ");
    JLabel Request = new JLabel("          Are you sure?");
        
    Box frame_temp_box = Box.createVerticalBox();
    Box Name_box = Box.createHorizontalBox();
    Box Category_box = Box.createHorizontalBox();
    Box Size_box = Box.createHorizontalBox();
    Box Color_box = Box.createHorizontalBox();
    Box Buy_Price_box = Box.createHorizontalBox();
    Box Picture_box = Box.createHorizontalBox();
    Box Quantity_box = Box.createHorizontalBox();
    Box Price_box = Box.createHorizontalBox();
    Box frame_temp_box2 = Box.createHorizontalBox();
    
    JButton Confirm_Button = new JButton(confirm_button_img);
    JButton Return_Button = new JButton(return_button_img);
            
    JLabel Error_logo = new JLabel();    
    JLabel CheckedOut_logo = new JLabel();
    JFrame Error_Frame = new JFrame("!");
    Container Error_contentPane = Error_Frame.getContentPane();
    Dimension X = new Dimension();
    Dimension Y = new Dimension();
    String Query = new String();
    Box Summary_box = Box.createVerticalBox();
    ResultSet Result = null;
    Box search_box = Box.createHorizontalBox();
    Box search_field_box = Box.createVerticalBox();
    JTextField search_field = new JTextField();
    // </editor-fold>
        
    public void Init_Sales(){
        search.addItem("Name");search.addItem("Category"); search.addItem("Size");search.addItem("Color"); 
        search_field_box.add(search);
        search_field_box.add(Box.createHorizontalStrut(10));
        search_field_box.add(search_field);
        search_box.add(search_field_box);
        
        frame_temp_box2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        frame_temp_box2.add(Box.createVerticalStrut(10));
        frame_temp_box2.add(Confirm_Button);
        frame_temp_box2.add(Box.createVerticalStrut(10));
        frame_temp_box2.add(Return_Button);
        frame_temp_box2.add(Box.createVerticalStrut(10));
        
        Summary.setEditable(false);
        Summary.setFont(new java.awt.Font("Verdana", 1, 14));
        Summary.setBackground(Color.lightGray);
        
//        Summary.setForeground(Color.white);
        X.setSize(330,140);
        Summary.setMinimumSize(X);
        Summary.setMaximumSize(X);
        scrollpane.setMinimumSize(X);
        scrollpane.setMaximumSize(X);
        Summary_box.add(scrollpane);
        Summary_box.setBounds(5, 5, 300, 220);
        Error_frame.setFont(new java.awt.Font("Verdana", 0, 30));
        frame_temp_box.setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 50));
        Inventory_table.setRowHeight(20);
        Cart_table.setRowHeight(20);
        Error_Frame.setSize(new Dimension(280, 130));
        Error_Frame.setResizable(false);
        Error_logo.setIcon(new javax.swing.ImageIcon("pictures/error/error_general.png"));
        Error_contentPane.setBackground(Color.white);
        Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
        Error_sales.setForeground(Color.red);
        Error_frame.setForeground(Color.red);
        
        CheckedOut_logo.setIcon(new javax.swing.ImageIcon("pictures/sales/checked_out.png"));
        
        Name_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Category_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Size_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Color_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Buy_Price_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Picture_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Quantity_label.setFont(new java.awt.Font("Verdana", 1, 15));
        Price_label.setFont(new java.awt.Font("Verdana", 1, 15));
        
        frame_temp_box.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        Name_box.add(Name_label);
        Name_box.add(Name_txt);
        Category_box.add(Category_label);
        Category_box.add(Category_txt);
        Size_box.add(Size_label);
        Size_box.add(Size_txt);
        Color_box.add(Color_label);
        Color_box.add(Color_txt);
        Buy_Price_box.add(Buy_Price_label);
        Buy_Price_box.add(Buy_Price_txt);
        Picture_box.add(Picture_label);
        Picture_box.add(Picture_txt);
        Quantity_box.add(Quantity_label);
        Quantity_box.add(Quantity_txt);
        Price_box.add(Price_label);
        Price_box.add(Price_txt);
        Error_box.add(Error_frame);
                
        Y.setSize(150,25);
        Name_txt.setMinimumSize(Y);
        Category_txt.setMinimumSize(Y);
        Size_txt.setMinimumSize(Y);
        Color_txt.setMinimumSize(Y);
        Buy_Price_txt.setMinimumSize(Y);
        Picture_txt.setMinimumSize(Y);
        Quantity_txt.setMinimumSize(Y);
        Price_txt.setMinimumSize(Y);
        
        Name_txt.setMaximumSize(Y);
        Category_txt.setMaximumSize(Y);
        Size_txt.setMaximumSize(Y);
        Color_txt.setMaximumSize(Y);
        Buy_Price_txt.setMaximumSize(Y);
        Picture_txt.setMaximumSize(Y);
        Quantity_txt.setMaximumSize(Y);
        Price_txt.setMaximumSize(Y);

        Return_Button.setRolloverIcon(return_buttonr_img);
        Return_Button.setPressedIcon(return_buttonp_img);
        Return_Button.setActionCommand("Return");
        Return_Button.addActionListener(new Return());
        Main_Menu.Button_Options(Return_Button);
        
        Confirm_Button.setRolloverIcon(confirm_buttonr_img);
        Confirm_Button.setPressedIcon(confirm_buttonp_img);
        Confirm_Button.setActionCommand("Confirm");
        Confirm_Button.addActionListener(new Confirm());
        Main_Menu.Button_Options(Confirm_Button);
        
        logo.setIcon(new javax.swing.ImageIcon("pictures/sales/YTL Logo (sales250).png"));
        Cart_label.setIcon(new javax.swing.ImageIcon("pictures/Inventory.Cart.png"));
        Inventory_label.setIcon(new javax.swing.ImageIcon("pictures/Inventory.Inventory.png"));
        
        Sales_Tables_box.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        Cart_scrollpane.setViewportView(Cart_table);
        Inventory_scrollpane.setViewportView(Inventory_table);

        Add_Button.setRolloverIcon(add_item_buttonr_img);
        Add_Button.setPressedIcon(add_item_buttonp_img);
        Add_Button.setActionCommand("Add Item");
        Add_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Add_Button);
        
        Delete_Button.setRolloverIcon(delete_item_buttonr_img);
        Delete_Button.setPressedIcon(delete_item_buttonp_img);
        Delete_Button.setActionCommand("Delete Item");
        Delete_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Delete_Button);
        
        Search_Button.setRolloverIcon(search_buttonr_img);
        Search_Button.setPressedIcon(search_buttonp_img);
        Search_Button.setActionCommand("Search Item");
        Search_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Search_Button);
        
        Custom_Price_Button.setRolloverIcon(custom_price_buttonr_img);
        Custom_Price_Button.setPressedIcon(custom_price_buttonp_img);
        Custom_Price_Button.setActionCommand("Custom Price");
        Custom_Price_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Custom_Price_Button);
        
        Check_Out_Button.setRolloverIcon(check_out_buttonr_img);
        Check_Out_Button.setPressedIcon(check_out_buttonp_img);
        Check_Out_Button.setActionCommand("Check Out Items");
        Check_Out_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Check_Out_Button);
        
        frame_template("Search");
    }
    
    protected JComponent Sales_Data() {
        Sales_Data_box.removeAll();
        Sales_Data_box.add(logo);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Add_Button);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Delete_Button);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Search_Button);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Custom_Price_Button);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Check_Out_Button);
        Sales_Data_box.add(Box.createVerticalStrut(5));
        Sales_Data_box.add(Error_sales);
        return Sales_Data_box;
    }

    protected JComponent Sales_Tables() {
        Sales_Tables_box.removeAll();
        Sales_Tables_box.add(Inventory_label);
        Sales_Tables_box.add(Box.createVerticalStrut(5));
        Sales_Tables_box.add(Inventory_scrollpane);
        Sales_Tables_box.add(Box.createVerticalStrut(29));
        Sales_Tables_box.add(Cart_label);
        Sales_Tables_box.add(Box.createVerticalStrut(5));
        Sales_Tables_box.add(Cart_scrollpane); 
        return Sales_Tables_box;
    }
    
    public class Action implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
            Reload();            
            if(str.equals("Add Item")){
                if (0 >= Inventory_table.getSelectedRow()){
                    Error();
                    Error_sales.setIcon(add_item_error_icon);
                    Main_Menu.frame.getContentPane().repaint();
                }
                else {
                    Button_Command = "Add";
                    frame_template("Add");
                    Sales_Frame.setTitle(Button_Command);
                    Sales_Frame.setVisible(true);
                }
            }
            else if(str.equals("Delete Item")){
                if (0 >= Cart_table.getSelectedRow()){
                    Error();
                    Error_sales.setIcon(delete_item_error_icon);
                    Main_Menu.frame.getContentPane().repaint();
                }
                else {
                Button_Command = "Delete";
                frame_template("Delete");
                Sales_Frame.setTitle(Button_Command);
                Sales_Frame.setVisible(true);
                }
            }
            else if(str.equals("Search Item")){
                Button_Command = "Search";
                frame_template("Search");
                Sales_Frame.setTitle(Button_Command);
                Sales_Frame.setVisible(true);
            }
            else if(str.equals("Custom Price")){
                if (0 >= Cart_table.getSelectedRow()){
                    Error();
                    Error_sales.setIcon(custom_price_error_icon);
                Main_Menu.frame.getContentPane().repaint();
                }
                else {
                Button_Command = "Price";
                frame_template("Price");
                Sales_Frame.setTitle(Button_Command);
                Sales_Frame.setVisible(true);
                }
            }
            else if(str.equals("Check Out Items")){
                Button_Command = "Check Out";
                frame_template("Check Out");
                Sales_Frame.setTitle(Button_Command);
                Sales_Frame.setVisible(true);
            }
        }
    }
    
    public void Reload(){
        Error_sales.setText(""); 
        Error_frame.setText(" ");
        Error_frame.setIcon(frame_spacer);
        Error_sales.setIcon(null);
        
        Name_txt.setText("");
        Category_txt.setText("");
        Size_txt.setText("");
        Color_txt.setText("");
        Buy_Price_txt.setText("");
        Picture_txt.setText("");
        Quantity_txt.setText("");
        Price_txt.setText("");
            
        Name_txt.setBackground(Color.white); 
        Category_txt.setBackground(Color.white);
        Size_txt.setBackground(Color.white);
        Color_txt.setBackground(Color.white);
        Buy_Price_txt.setBackground(Color.white);
        Picture_txt.setBackground(Color.white);
        Quantity_txt.setBackground(Color.white);
        Price_txt.setBackground(Color.white);
        
        Inventory_table.repaint();
        Cart_table.repaint();
        
        frame_template("Add Item");
        Sales_Frame.repaint();
    }
    
    public class Confirm implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
            Error_sales.setText(""); 
            Error_frame.setText(" ");
            Error_frame.setIcon(frame_spacer);
            if(Size_txt.getText().equals(""))Size_txt.setText(null);
            if(Color_txt.getText().equals(""))Color_txt.setText(null);
            if(Picture_txt.getText().equals(""))Picture_txt.setText("Default.jpg");
            
            if(Button_Command.equals("Add")){
                if(Quantity_txt.getText().equals("")){
                    Error_frame.setIcon(blank_fields_error_icon);
                    Error();
                }
                else if(Math.round(Float.valueOf(Quantity_txt.getText())) <= 0 || Math.round(Float.valueOf(Quantity_txt.getText())) > Integer.valueOf(Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 6).toString()) || Integer.valueOf(Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 6).toString()) <= 0)
                {
                    Error_frame.setIcon(invalid_input_error_icon);
                    Error();
                }
                else if(Inventory_table.getSelectedRow() > 0 ){
                    Cart_model.insertRow(Cart_model.getRowCount(),new Object[]{Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 4), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 5), Math.round(Float.valueOf(Quantity_txt.getText())), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 7)});
                    Inventory.Inventory_model.insertRow(Inventory_table.getSelectedRow()+1,new Object[]{Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 4), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 5),  (Integer.parseInt(Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 6).toString()) - Math.round(Float.valueOf(Quantity_txt.getText()))) , Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 7)});
                    Inventory.Inventory_model.removeRow(Inventory_table.getSelectedRow());
                    Sales_Frame.setVisible(false);
                    Reload();
                }
            }
            else if(Button_Command.equals("Delete")){
                if(Cart_table.getSelectedRow() > 0 ){

                    xtr = Inventory_table.getRowCount();
                    i=1;
                    while(i < xtr){
                       try{
                        if(Inventory_table.getValueAt(i, 0).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 0))&&Inventory_table.getValueAt(i, 1).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 1))&&Inventory_table.getValueAt(i, 2).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 2))&&Inventory_table.getValueAt(i, 3).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 3))&&Inventory_table.getValueAt(i, 4).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 4))&&Inventory_table.getValueAt(i, 5).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 5))&&Inventory_table.getValueAt(i, 7).equals(Cart_table.getValueAt(Cart_table.getSelectedRow(), 7)))
                        {   
                            Inventory.Inventory_model.insertRow(i+1,new Object[]{Inventory_table.getValueAt(i, 0), Inventory_table.getValueAt(i, 1), Inventory_table.getValueAt(i, 2), Inventory_table.getValueAt(i, 3), Inventory_table.getValueAt(i, 4), Inventory_table.getValueAt(i, 5),  (Integer.parseInt(Inventory_table.getValueAt(i, 6).toString()) + Integer.parseInt(Cart_table.getValueAt(Cart_table.getSelectedRow(),6).toString())) , Inventory_table.getValueAt(i, 7)});
                            Inventory.Inventory_model.removeRow(i);
                        }}catch(NullPointerException e){}
                        i++;
                    }
                    Cart_model.removeRow(Cart_table.getSelectedRow());
                    
                    Sales_Frame.setVisible(false);
                    Reload();
                }
            }
            else if(Button_Command.equals("Search")){
                i = 1;
                if ( 1 <= Inventory_table.getRowCount())
                    while(i < Inventory_table.getRowCount()){
                        
                       try{
                        if(Inventory_table.getValueAt(i, search.getSelectedIndex()).equals(search_field.getText()))
                        {   
                            Inventory.Inventory_model.insertRow(1,new Object[]{Inventory_table.getValueAt(i, 0), Inventory_table.getValueAt(i, 1), Inventory_table.getValueAt(i, 2), Inventory_table.getValueAt(i, 3), Inventory_table.getValueAt(i, 4),Inventory_table.getValueAt(i, 5),Inventory_table.getValueAt(i, 6),Inventory_table.getValueAt(i, 7)});
                            Inventory.Inventory_model.removeRow(i+1);
                        }}catch(NullPointerException e){}
                        i++;
                    }
                    Reload();
                    Sales_Frame.setVisible(false);                
            }
            else if(Button_Command.equals("Price")){
                if(Cart_table.getSelectedRow() > 0 ){
                    Price_new = Price_txt.getText();
                    Cart_model.setValueAt("Php " + Price_new, Cart_table.getSelectedRow(), 7);
                    Sales_Frame.setVisible(false);
                    Reload();
                }
            }
            else if(Button_Command.equals("Check Out")){
                Sales_Frame.setVisible(false);
                x = Cart_model.getRowCount();
                for(ctr = 0; ctr < x - 1; ctr++)
                    Cart_model.removeRow(1);
                Reload();                
                CheckedOut();
            }
        }
    }
    
    public class Return implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
        Sales_Frame.setVisible(false);
        Reload();
        }
    }
    
    public void frame_template(String Command){
        
        Sales_Frame_container.removeAll();
        subTotal = 0;
        Total = 0;
        Sales_Frame.repaint();
        Error_frame.setText(" ");
        Summary.setText("");
        Price_new = "0";
        Button_Command = Command;
        pt = Main_Menu.frame.getLocation();
        Sales_Frame.setLocation(pt.x + 320,pt.y + 100);
        D.setSize(330, 300);
        Sales_Frame.setSize(D);
        Sales_Frame.setResizable(true);
        
        if("Delete".equals(Button_Command) || "Price".equals(Button_Command) || "Add".equals(Button_Command) || "Search".equals(Button_Command)){
            D.setSize(330, 300);
            Sales_Frame.setSize(D);
            Sales_Frame.setResizable(false);
        }
        
        frame_temp_box.removeAll();
        
        if("Add".equals(Button_Command)){
            Summary.append("\n Would you like to add this item?\n");
            Summary.append("  Name: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0) + "\n");
            Summary.append("  Category: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1) + "\n");
            Summary.append("  Size: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2) + "\n");
            Summary.append("  Color: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3)+ "\n");
            frame_temp_box.add(Summary);
            frame_temp_box.add(Quantity_box);
        }
        if("Delete".equals(Button_Command)){
            Summary.append("\n Do you want to remove this item?\n");
            Summary.append("  Name: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 0) + "\n");
            Summary.append("  Category: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 1) + "\n");
            Summary.append("  Size: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 2) + "\n");
            Summary.append("  Color: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 3)+"\n");
            frame_temp_box.add(Summary);
        }
        if("Price".equals(Button_Command)){
            Summary.append("\n Adjust this item's price to what?\n");
            Summary.append("  Name: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 0) + "\n");
            Summary.append("  Size: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 2) + "\n");
            Summary.append("  Color: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 3)+ "\n");
            Summary.append("  Purchase Price: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 4)+ "\n");
            Summary.append("  Original Price: " + Cart_table.getValueAt(Cart_table.getSelectedRow(), 7));
            frame_temp_box.add(Summary);
            frame_temp_box.add(Price_box);
        }
        if("Search".equals(Button_Command)){
            search_field.setText("");
            frame_temp_box.removeAll();
            frame_temp_box.add(search_box);
        }
        if("Check Out".equals(Button_Command)){
            
            j = 1;
            
            
            while(j < Cart_table.getRowCount()){
                subTotal = Float.parseFloat(Cart_table.getValueAt(j, 7).toString().substring(4)) * Float.parseFloat(Cart_table.getValueAt(j, 6).toString());
                Total += subTotal;
                Summary.append("  " + Cart_table.getValueAt(j, 1) + " \n     " + Cart_table.getValueAt(j, 7) + " x " + Cart_table.getValueAt(j, 6) + " = " + subTotal +"\n"); 
                
                try{
                Result = null;
//                System.out.print("Select * from inventory WHERE inventory.Name ='"+ Cart_model.getValueAt(j, 0).toString() +"' AND inventory.Category ='"+ Cart_model.getValueAt(j, 1).toString() +"' AND inventory.Size ='' AND inventory.Color ='"+ Cart_model.getValueAt(j, 3).toString() +"'AND inventory.PurchasePrice ='"+ Cart_model.getValueAt(j, 4).toString().substring(4) +"' AND inventory.Picture ='"+ Cart_model.getValueAt(j, 5).toString() +"' AND inventory.SellingPrice ='"+ Cart_model.getValueAt(j, 7).toString().substring(4) +"';");
//                Result = Main_Menu.DBConnection.Query_Retrieve("Select * from inventory WHERE inventory.Name ='"+ Cart_model.getValueAt(j, 0).toString() +"' AND inventory.Category ='"+ Cart_model.getValueAt(j, 1).toString() +"' AND inventory.Size ='' AND inventory.Color ='"+ Cart_model.getValueAt(j, 3).toString() +"'AND inventory.PurchasePrice ='"+ Cart_model.getValueAt(j, 4).toString().substring(4) +"' AND inventory.Picture ='"+ Cart_model.getValueAt(j, 5).toString() +"' AND inventory.SellingPrice ='"+ Cart_model.getValueAt(j, 7).toString().substring(4) +"';");
                Result = Main_Menu.DBConnection.Query_Retrieve("Select * from product WHERE product.Name ='"+ Cart_model.getValueAt(j, 0).toString() +"' AND product.Category ='"+ Cart_model.getValueAt(j, 1).toString() +"' AND product.PurchasePrice ='"+ Cart_model.getValueAt(j, 4).toString().substring(4) +"' AND product.SellingPrice ='"+ Cart_model.getValueAt(j, 7).toString().substring(4) +"';");
                
                Result.first();
                //System.out.print("INSERT INTO `[ytl]`.`sales` (`ProductID`, `ActualPrice`, `ActualTax`, `Quantity`, `Profit`) VALUES ('"+ Result.getString(1) +"', '"+ subTotal +"', '"+ (subTotal * Main_Menu.DBConnection.Tax )+"', '"+ Integer.parseInt(Cart_model.getValueAt(j, 6).toString()) +"', '"+ (subTotal - ((Float.parseFloat(Result.getString(7)) + Float.parseFloat(Result.getString(9)))*Integer.parseInt(Cart_model.getValueAt(j, 6).toString()))) +"');");
                Main_Menu.DBConnection.Query_Send("INSERT INTO `[ytl]`.`sales` (`ProductID`, `ActualPrice`, `ActualTax`, `Quantity`, `Profit`) VALUES ('"+ Result.getString(1) +"', '"+ subTotal +"', '"+ (subTotal * Main_Menu.DBConnection.Tax )+"', '"+ Integer.parseInt(Cart_model.getValueAt(j, 6).toString()) +"', '"+ Float.parseFloat(String.format("%.2f", subTotal - ((Float.parseFloat(Result.getString(7)) + Float.parseFloat(Result.getString(9)))*Integer.parseInt(Cart_model.getValueAt(j, 6).toString())))) +"');");
                Main_Menu.DBConnection.DBClose();
                
                Main_Menu.DBConnection.Query_Send("UPDATE product SET Quantity = '"+ (Integer.parseInt(Result.getString(11)) - Integer.parseInt(Cart_model.getValueAt(j, 6).toString()))+"' WHERE product.ProductID = '"+ Result.getString(1) +"';");
                Main_Menu.DBConnection.DBClose();
                
                
                } catch (ClassNotFoundException ex) {
                        Logger.getLogger(Sales.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SQLException ex) {
                        Logger.getLogger(Sales.class.getName()).log(Level.SEVERE, null, ex);
                    }
                j++;
            }
            
                Summary.append("\n  Total = " + Total);
                frame_temp_box.add(Summary);
        }
        frame_temp_box.add(Error_box);
        
        
        
        Sales_Frame_container.setBackground(Color.LIGHT_GRAY);
        Sales_Frame_container.add(frame_temp_box,BorderLayout.BEFORE_LINE_BEGINS);
        Sales_Frame_container.add(frame_temp_box2,BorderLayout.PAGE_END);
        Sales_Frame_container.repaint();
    }
    
    public static void Sales() {
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Sales();
            }
        });
    }
    
    public void Error(){
        pt = Main_Menu.frame.getLocation();
        Error_Frame.setLocation(pt.x + 280,pt.y + 140);
        try{
            Error_thread threads = new Error_thread();
            threads.start();
        }catch(IllegalThreadStateException e){
        }
    }
    
    class Error_thread extends Thread {
        
        @Override
        public void run() {
            try {
                Error_contentPane.removeAll();
                Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
                Error_Frame.setVisible(true);
                if(Main_Menu.Systems.sound==true)
                try {
                AudioClip no = Applet.newAudioClip(new URL("file:pictures/no.wav"));
                no.play();
                } catch (MalformedURLException ex) {
                }
                Thread.sleep(1000);
                Error_Frame.setVisible(false);
            } catch (InterruptedException e) {
    }}}
    
     public void CheckedOut(){
        pt = Main_Menu.frame.getLocation();
        Error_Frame.setLocation(pt.x + 280,pt.y + 140);
        try{
            CheckedOut_thread threads = new CheckedOut_thread();
            threads.start();
        }catch(IllegalThreadStateException e){
        }
    }
    
    class CheckedOut_thread extends Thread {
        
        @Override
        public void run() {
            try {
                Error_contentPane.removeAll();
                Error_contentPane.add(CheckedOut_logo,BorderLayout.BEFORE_LINE_BEGINS);
                Error_Frame.setVisible(true);
                
                Toolkit.getDefaultToolkit().beep();
                
                Thread.sleep(1000);
                Error_Frame.setVisible(false);
            } catch (InterruptedException e) {            
    }}}
}
